<template><div><h2 id="从-yii-1-1-升级" tabindex="-1"><a class="header-anchor" href="#从-yii-1-1-升级"><span>从 Yii 1.1 升级</span></a></h2>
<p>2.0 版框架是完全重写的，在 1.1 和 2.0 两个版本之间存在相当多差异。 因此从 1.1 版升级并不像小版本间的跨越那么简单， 通过本指南你将会了解两个版本间主要的不同之处。</p>
<p>如果你之前没有用过 Yii 1.1，可以跳过本章，直接从”<a href="https://learnku.com/docs/yii-framework/2.0.x/what-do-you-need-to-know/11953" target="_blank" rel="noopener noreferrer">入门篇</a>“开始读起。</p>
<p>请注意，Yii 2.0 引入了很多本章并没有涉及到的新功能。 强烈建议你通读整部权威指南来了解所有新特性。 这样有可能会发现一些以前你要自己开发的功能，而现在已经被包含在核心代码中了。</p>
<h2 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h2>
<p>Yii 2.0 完全拥抱 <a href="https://getcomposer.org/" target="_blank" rel="noopener noreferrer">Composer</a>，它是事实上的 PHP 依赖管理工具。 核心框架以及扩展的安装都通过 Composer 来处理。想要了解更多如何安装 Yii 2.0 请参阅本指南的 <a href="https://learnku.com/docs/yii-framework/2.0.x/installation-yii/11954" target="_blank" rel="noopener noreferrer">安装 Yii</a> 章节。如果你想创建新扩展， 或者把你已有的 Yii 1.1 的扩展改写成兼容 2.0 的版本， 你可以参考 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-extensions.md#creating-extensions" target="_blank" rel="noopener noreferrer">创建扩展</a> 章节。</p>
<h2 id="php-需求" tabindex="-1"><a class="header-anchor" href="#php-需求"><span>PHP 需求</span></a></h2>
<p>Yii 2.0 需要 PHP 5.4 或更高版本，该版本相对于 Yii 1.1 所需求的 PHP 5.2 而言有巨大的改进。 因此在语言层面上有很多的值得注意的不同之处。 下面是 PHP 层的主要变化汇总：</p>
<ul>
<li><a href="https://www.php.net/manual/zh/language.namespaces.php" target="_blank" rel="noopener noreferrer">命名空间</a></li>
<li><a href="https://www.php.net/manual/zh/functions.anonymous.php" target="_blank" rel="noopener noreferrer">匿名函数</a></li>
<li>数组短语法 <code v-pre>[...元素...]</code> 用于取代 <code v-pre>array(...元素...)</code></li>
<li>视图文件中的短格式 echo 标签 <code v-pre>&lt;?=</code>，自 PHP 5.4 起总会被识别并且合法，无论 short_open_tag 的设置是什么，可以安全使用。</li>
<li><a href="https://www.php.net/manual/zh/book.spl.php" target="_blank" rel="noopener noreferrer">SPL 类和接口</a></li>
<li><a href="https://www.php.net/manual/zh/language.oop5.late-static-bindings.php" target="_blank" rel="noopener noreferrer">延迟静态绑定</a></li>
<li><a href="https://www.php.net/manual/zh/book.datetime.php" target="_blank" rel="noopener noreferrer">日期和时间</a></li>
<li><a href="https://www.php.net/manual/zh/language.oop5.traits.php" target="_blank" rel="noopener noreferrer">Traits</a></li>
<li><a href="https://www.php.net/manual/zh/book.intl.php" target="_blank" rel="noopener noreferrer">intl</a> Yii 2.0 使用 PHP 扩展 <code v-pre>intl</code> 来支持国际化的相关功能。</li>
</ul>
<h2 id="命名空间" tabindex="-1"><a class="header-anchor" href="#命名空间"><span>命名空间</span></a></h2>
<p>Yii 2.0 里最明显的改动就数命名空间的使用了。几乎每一个核心类都引入了命名空间， 比如 <code v-pre>yii\web\Request</code>。1.1 版类名前缀 “C” 已经不再使用。 当前的命名方案与目录结构相吻合。例如，<code v-pre>yii\web\Request</code> 就表明对应的类文件是 Yii 框架文件夹下的 <code v-pre>web/Request.php</code> 文件。</p>
<p>（有了 Yii 的类自动加载器， 你可以直接使用全部核心类而不需要显式包含具体文件。）</p>
<h2 id="组件-component-与对象-baseobject" tabindex="-1"><a class="header-anchor" href="#组件-component-与对象-baseobject"><span>组件（Component）与对象（BaseObject）</span></a></h2>
<p>Yii 2.0 把 1.1 中的 <code v-pre>CComponent</code> 类拆分成了两个类：[[yii\base\Object]] 和 [[yii\base\Component]]。 [[yii\base\Object|Object]] 类是一个轻量级的基类，你可以通过 getters 和 setters 来定义<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-properties.md" target="_blank" rel="noopener noreferrer">对象的属性</a>。 [[yii\base\Component|Component]] 类继承自 [[yii\base\Object|Object]]， 同时进一步支持 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-events.md" target="_blank" rel="noopener noreferrer">事件</a> 和 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-behaviors.md" target="_blank" rel="noopener noreferrer">行为</a>。</p>
<p>如果你不需要用到事件或行为， 应该考虑使用 [[yii\base\Object|Object]] 类作为基类。 这种类通常用来表示基本的数据结构。</p>
<h2 id="对象的配置" tabindex="-1"><a class="header-anchor" href="#对象的配置"><span>对象的配置</span></a></h2>
<p>[[yii\base\Object|Object]] 类引入了一种统一对象配置的方法。 所有 [[yii\base\Object|Object]] 的子类都应该用以下方法声明它的构造方法（如果需要的话）， 以正确配置它自身：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">MyClass</span> <span class="token keyword">extends</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>base<span class="token punctuation">\</span>BaseObject</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">__construct</span><span class="token punctuation">(</span><span class="token variable">$param1</span><span class="token punctuation">,</span> <span class="token variable">$param2</span><span class="token punctuation">,</span> <span class="token variable">$config</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// ... 配置生效前的初始化过程</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword static-context">parent</span><span class="token operator">::</span><span class="token function">__construct</span><span class="token punctuation">(</span><span class="token variable">$config</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword static-context">parent</span><span class="token operator">::</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// ... 配置生效后的初始化过程</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在上面的例子里，构造方法的最后一个参数必须传入一个配置数组， 包含一系列用于在方法结尾初始化相关属性的键值对。 你可以重写 [[yii\base\Object::init()|init()]] 方法来执行一些需要在配置生效后进行的初始化工作。</p>
<p>你可以通过遵循以下约定俗成的编码习惯， 来使用配置数组创建并配置新的对象：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$object</span> <span class="token operator">=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token function">createObject</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'MyClass'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'property1'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'abc'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'property2'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'cde'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token variable">$param1</span><span class="token punctuation">,</span> <span class="token variable">$param2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>更多有关配置的细节可以在<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-configurations.md" target="_blank" rel="noopener noreferrer">配置</a>章节找到。</p>
<h2 id="事件-event" tabindex="-1"><a class="header-anchor" href="#事件-event"><span>事件（Event）</span></a></h2>
<p>在 Yii 1 中，通常通过定义 <code v-pre>on</code> 开头的方法（例如 <code v-pre>onBeforeSave</code>）来创建事件。 而在 Yii 2 中，你可以使用任意的事件名了。同时通过调用 [[yii\base\Component::trigger()|trigger()]] 方法来触发相关事件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$event</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>base<span class="token punctuation">\</span>Event</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$component</span><span class="token operator">-></span><span class="token function">trigger</span><span class="token punctuation">(</span><span class="token variable">$eventName</span><span class="token punctuation">,</span> <span class="token variable">$event</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>要给事件附加一个事件事件处理器，需要使用 [[yii\base\Component::on()|on()]] 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$component</span><span class="token operator">-></span><span class="token function">on</span><span class="token punctuation">(</span><span class="token variable">$eventName</span><span class="token punctuation">,</span> <span class="token variable">$handler</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">// 解除事件处理器，使用 off 方法：</span></span>
<span class="line"><span class="token comment">// $component->off($eventName, $handler);</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>事件功能还有更多增强之处。要了解它们，请查看<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-events.md" target="_blank" rel="noopener noreferrer">事件</a>章节。</p>
<h2 id="路径别名-path-alias" tabindex="-1"><a class="header-anchor" href="#路径别名-path-alias"><span>路径别名（Path Alias）</span></a></h2>
<p>Yii 2.0 将路径别名的应用扩大至文件/目录路径和 URL。Yii 2.0 中路径别名必须以 <code v-pre>@</code> 符号开头， 以区别于普通文件目录路径或 URL。例如 <code v-pre>@yii</code> 就是指向 Yii 安装目录的别名。 绝大多数 Yii 核心代码都支持别名。 例如 [[yii\caching\FileCache::cachePath]] 就同时支持路径别名或普通的目录地址。</p>
<p>路径别名也和类的命名空间密切相关。建议给每一个根命名空间定义一个路径别名， 从而无须额外配置，便可启动 Yii 的类自动加载机制。 例如，因为有 <code v-pre>@yii</code> 指向 Yii 安装目录， 那类似 <code v-pre>yii\web\Request</code> 的类就能被 Yii 自动加载。同理，若你用了一个第三方的类库， 如 Zend Framework，你只需定义一个名为 <code v-pre>@Zend</code> 的路径别名指向该框架的安装目录。 之后 Yii 就可以自动加载任意 Zend Framework 中的类了。</p>
<p>更多路径别名信息请参阅<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-aliases.md" target="_blank" rel="noopener noreferrer">路径别名</a>章节。</p>
<h2 id="视图-view" tabindex="-1"><a class="header-anchor" href="#视图-view"><span>视图（View）</span></a></h2>
<p>Yii 2 中视图最明显的改动是视图内的特殊变量 <code v-pre>$this</code> 不再指向当前控制器或小部件， 而是指向<strong>视图</strong>对象，它是 2.0 中引入的全新概念。<strong>视图</strong>对象为 [[yii\web\View]] 的实例， 他代表了 MVC 模式中的视图部分。如果你想要在视图中访问一个控制器或小部件， 可以使用 <code v-pre>$this-&gt;context</code>。</p>
<p>要在其他视图里渲染一个局部视图，使用 <code v-pre>$this-&gt;render()</code>，而不是 <code v-pre>$this-&gt;renderPartial()</code>。 <code v-pre>render()</code> 现在只返回渲染结果，而不是直接显示它，所以现在你必须显式地把它 <strong>echo</strong> 出来。像这样：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">render</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'_item'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'item'</span> <span class="token operator">=></span> <span class="token variable">$item</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>除了使用 PHP 作为主要的模板语言，Yii 2.0 也装备了两种流行模板引擎的官方支持：Smarty 和 Twig。 过去的 Prado 模板引擎不再被支持。要使用这些模板引擎， 你需要配置 <code v-pre>view</code> 应用组件， 给它设置 [[yii\base\View::$renderers|View::$renderers]] 属性。 具体请参阅<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/tutorial-template-engines.md" target="_blank" rel="noopener noreferrer">模板引擎</a>章节。</p>
<h2 id="模型-model" tabindex="-1"><a class="header-anchor" href="#模型-model"><span>模型（Model）</span></a></h2>
<p>Yii 2.0 使用 [[yii\base\Model]] 作为模型基类，类似于 1.1 的 <code v-pre>CModel</code> 。 <code v-pre>CFormModel</code> 被完全弃用了，现在要创建表单模型类，可以通过继承 [[yii\base\Model]] 类来实现。</p>
<p>Yii 2.0 引进了名为 [[yii\base\Model::scenarios()|scenarios()]] 的新方法来声明支持的场景， 并指明在哪个场景下某属性必须经过验证，可否被视为安全值等等。如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">scenarios</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'backend'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'role'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'frontend'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'!role'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>上面的代码声明了两个场景：<code v-pre>backend</code> 和 <code v-pre>frontend</code> 。对于 <code v-pre>backend</code> 场景，<code v-pre>email</code> 和 <code v-pre>role</code> 属性值都是安全的， 且能进行批量赋值。对于 <code v-pre>frontend</code> 场景，<code v-pre>email</code> 能批量赋值而 <code v-pre>role</code> 不能。 <code v-pre>email</code> 和 <code v-pre>role</code> 都必须通过规则验证。</p>
<p>[[yii\base\Model::rules()|rules()]] 方法仍用于声明验证规则。注意，由于引入了 [[yii\base\Model::scenarios()|scenarios()]]，现在已经没有 <code v-pre>unsafe</code> 验证器了。</p>
<p>大多数情况下，如果 [[yii\base\Model::rules()|rules()]] 方法内已经完整地指定场景了， 那就不必覆写 [[yii\base\Model::scenarios()|scenarios()]]， 也不必声明 <code v-pre>unsafe</code> 属性值。</p>
<p>要了解更多有关模型的细节，请参考<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-models.md" target="_blank" rel="noopener noreferrer">模型</a>章节。</p>
<h2 id="控制器-controller" tabindex="-1"><a class="header-anchor" href="#控制器-controller"><span>控制器（Controller）</span></a></h2>
<p>Yii 2.0 使用 [[yii\web\Controller]] 作为控制器的基类， 它类似于 1.1 的 <code v-pre>CController</code>。使用 [[yii\base\Action]] 作为操作类的基类。</p>
<p>这些变化最明显的影响是，当你在写控制器操作的代码时， 应该返回（return）要渲染的内容而不是输出（echo）它：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">actionView</span><span class="token punctuation">(</span><span class="token variable">$id</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$model</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>app<span class="token punctuation">\</span>models<span class="token punctuation">\</span>Post</span><span class="token operator">::</span><span class="token function">findOne</span><span class="token punctuation">(</span><span class="token variable">$id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$model</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">render</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'view'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'model'</span> <span class="token operator">=></span> <span class="token variable">$model</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>web<span class="token punctuation">\</span>NotFoundHttpException</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>请查看<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-controllers.md" target="_blank" rel="noopener noreferrer">控制器（Controller）</a>章节了解有关控制器的更多细节。</p>
<h2 id="小部件-widget" tabindex="-1"><a class="header-anchor" href="#小部件-widget"><span>小部件（Widget）</span></a></h2>
<p>Yii 2.0 使用 [[yii\base\Widget]] 作为小部件基类，类似于 1.1 的 <code v-pre>CWidget</code>。</p>
<p>为了让框架获得更好的 IDE 支持，Yii 2.0 引进了一个调用小部件的新语法。 包含 [[yii\base\Widget::begin()|begin()]]，[[yii\base\Widget::end()|end()]] 和 [[yii\base\Widget::widget()|widget()]] 三个静态方法，用法如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>widgets<span class="token punctuation">\</span>Menu</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>widgets<span class="token punctuation">\</span>ActiveForm</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 注意必须 **"echo"** 结果以显示内容</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token class-name static-context">Menu</span><span class="token operator">::</span><span class="token function">widget</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'items'</span> <span class="token operator">=></span> <span class="token variable">$items</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 传递一个用于初始化对象属性的数组</span></span>
<span class="line"><span class="token variable">$form</span> <span class="token operator">=</span> <span class="token class-name static-context">ActiveForm</span><span class="token operator">::</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'options'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'form-horizontal'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'fieldConfig'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'inputOptions'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'input-xlarge'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token operator">...</span> 表单输入栏都在这里 <span class="token operator">...</span></span>
<span class="line"><span class="token class-name static-context">ActiveForm</span><span class="token operator">::</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>更多细节请参阅<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-widgets.md" target="_blank" rel="noopener noreferrer">小部件</a>章节。</p>
<h2 id="主题-theme" tabindex="-1"><a class="header-anchor" href="#主题-theme"><span>主题（Theme）</span></a></h2>
<p>2.0 主题的运作方式跟以往完全不同了。它们现在基于<strong>路径映射机制</strong>，该机制会把一个源视图文件的路径映射到一个主题视图文件路径。 举例来说，如果路径映射为 <code v-pre>['/web/views' =&gt; '/web/themes/basic']</code>， 那么 <code v-pre>/web/views/site/index.php</code> 视图经过主题修饰的版本就会是 <code v-pre>/web/themes/basic/site/index.php</code>。 也因此让主题现在可以应用在任何视图文件之上， 甚至是渲染控制器上下文环境之外的视图文件或小部件。</p>
<p>同样，<code v-pre>CThemeManager</code> 组件已经被移除了。 取而代之的 <code v-pre>theme</code> 成为了 <code v-pre>view</code> 应用组件的一个可配置属性。</p>
<p>更多细节请参考<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/output-theming.md" target="_blank" rel="noopener noreferrer">主题</a>章节。</p>
<h2 id="控制台应用-console-application" tabindex="-1"><a class="header-anchor" href="#控制台应用-console-application"><span>控制台应用（Console Application）</span></a></h2>
<p>控制台应用现在如普通的 Web 应用程序一样，由控制器组成， 控制台的控制器继承自 [[yii\console\Controller]]，类似于 1.1 的 <code v-pre>CConsoleCommand</code>。</p>
<p>运行控制台命令使用 <code v-pre>yii &lt;route&gt;</code>， 其中 <code v-pre>&lt;route&gt;</code> 代表控制器的路由（如 <code v-pre>sitemap/index</code>）。 额外的匿名参数传递到对应的控制器操作方法， 而有名的参数根据 [[yii\console\Controller::options()]] 的声明来解析。</p>
<p>Yii 2.0 支持基于代码注释自动生成相的关命令行帮助（help）信息。</p>
<p>更多细节请参阅<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/tutorial-console.md" target="_blank" rel="noopener noreferrer">控制台命令</a>章节。</p>
<h2 id="国际化-i18n" tabindex="-1"><a class="header-anchor" href="#国际化-i18n"><span>国际化（I18N）</span></a></h2>
<p>Yii 2.0 移除了原来内置的日期格式器和数字格式器，为了支持 <a href="https://pecl.php.net/package/intl" target="_blank" rel="noopener noreferrer">PECL intl PHP module</a>（PHP 的国际化扩展）的使用。</p>
<p>消息翻译现在由 <code v-pre>i18n</code> 应用组件执行。 该组件管理一系列消息源， 允许使用基于消息类别的不同消息源。</p>
<p>更多细节请参阅<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/tutorial-i18n.md" target="_blank" rel="noopener noreferrer">国际化（Internationalization）</a>章节。</p>
<h2 id="动作过滤器-action-filters" tabindex="-1"><a class="header-anchor" href="#动作过滤器-action-filters"><span>动作过滤器（Action Filters）</span></a></h2>
<p>操作的过滤现在通过行为（behavior）来实现。要定义一个新的，自定义的过滤器，请继承 [[yii\base\ActionFilter]] 类。 要使用一个过滤器，需要把过滤器类作为一个 <code v-pre>behavior</code> 绑定到控制器上。 例如，要使用 [[yii\filters\AccessControl]] 过滤器，你需要在控制器内添加如下代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">behaviors</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'access'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\filters\AccessControl'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'rules'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token punctuation">[</span><span class="token string single-quoted-string">'allow'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'actions'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'admin'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'roles'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'@'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>更多细节请参考<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-filters.md" target="_blank" rel="noopener noreferrer">过滤器</a>章节。</p>
<h2 id="前端资源-assets" tabindex="-1"><a class="header-anchor" href="#前端资源-assets"><span>前端资源（Assets）</span></a></h2>
<p>Yii 2.0 引入了一个新的概念，称为<strong>资源包</strong>（Asset Bundle），以代替 1.1 的脚本包概念。</p>
<p>一个资源包是一个目录下的资源文件集合（如 JavaScript 文件、CSS 文件、图片文件等）。 每一个资源包被表示为一个类，该类继承自 [[yii\web\AssetBundle]]。 用 [[yii\web\AssetBundle::register()]] 方法注册一个资源包后， 就使它的资源可被 Web 访问了， 注册了资源包的页面会自动包含和引用资源包内指定的 JS 和 CSS 文件。</p>
<p>更多细节请参阅 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-assets.md" target="_blank" rel="noopener noreferrer">前端资源管理（Asset）</a> 章节。</p>
<h2 id="助手类-helpers" tabindex="-1"><a class="header-anchor" href="#助手类-helpers"><span>助手类（Helpers）</span></a></h2>
<p>Yii 2.0 很多常用的静态助手类，包括：</p>
<ul>
<li>[[yii\helpers\Html]]</li>
<li>[[yii\helpers\ArrayHelper]]</li>
<li>[[yii\helpers\StringHelper]]</li>
<li>[[yii\helpers\FileHelper]]</li>
<li>[[yii\helpers\Json]]</li>
</ul>
<p>请参考<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/helper-overview.md" target="_blank" rel="noopener noreferrer">助手一览</a> 章节来了解更多。</p>
<h2 id="表单-forms" tabindex="-1"><a class="header-anchor" href="#表单-forms"><span>表单（Forms）</span></a></h2>
<p>Yii 2.0 引进了**表单栏（field）**的概念，用来创建一个基于 [[yii\widgets\ActiveForm]] 的表单。 一个表单栏是一个由标签、输入框、错误消息（可能还有提示文字）组成的容器， 被表示为一个 [[yii\widgets\ActiveField|ActiveField]] 对象。 使用表单栏建立表单的过程比以前更整洁利落：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span> <span class="token variable">$form</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">yii<span class="token punctuation">\</span>widgets<span class="token punctuation">\</span>ActiveForm</span><span class="token operator">::</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token delimiter important">?></span></span></span>
<span class="line">    <span class="token php language-php"><span class="token delimiter important">&lt;?=</span> <span class="token variable">$form</span><span class="token operator">-></span><span class="token function">field</span><span class="token punctuation">(</span><span class="token variable">$model</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'username'</span><span class="token punctuation">)</span> <span class="token delimiter important">?></span></span></span>
<span class="line">    <span class="token php language-php"><span class="token delimiter important">&lt;?=</span> <span class="token variable">$form</span><span class="token operator">-></span><span class="token function">field</span><span class="token punctuation">(</span><span class="token variable">$model</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'password'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">passwordInput</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token delimiter important">?></span></span></span>
<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>form-group<span class="token punctuation">"</span></span><span class="token punctuation">></span></span></span>
<span class="line">        <span class="token php language-php"><span class="token delimiter important">&lt;?=</span> <span class="token class-name static-context">Html</span><span class="token operator">::</span><span class="token function">submitButton</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Login'</span><span class="token punctuation">)</span> <span class="token delimiter important">?></span></span></span>
<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span></span>
<span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span> <span class="token class-name class-name-fully-qualified static-context">yii<span class="token punctuation">\</span>widgets<span class="token punctuation">\</span>ActiveForm</span><span class="token operator">::</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token delimiter important">?></span></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>请参考<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/input-forms.md" target="_blank" rel="noopener noreferrer">创建表单</a>章节来了解更多细节。</p>
<h2 id="查询生成器-query-builder" tabindex="-1"><a class="header-anchor" href="#查询生成器-query-builder"><span>查询生成器（Query Builder）</span></a></h2>
<p>Yii 1.1 中，查询语句的生成分散在多个类中，包括 <code v-pre>CDbCommand</code>，<code v-pre>CDbCriteria</code> 以及 <code v-pre>CDbCommandBuilder</code>。 Yii 2.0 以 [[yii\db\Query|Query]] 对象的形式表示一个数据库查询， 这个对象使用 [[yii\db\QueryBuilder|QueryBuilder]] 在幕后生成 SQL 语句。 例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$query</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>db<span class="token punctuation">\</span>Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$query</span><span class="token operator">-></span><span class="token function">select</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id, name'</span><span class="token punctuation">)</span></span>
<span class="line">      <span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user'</span><span class="token punctuation">)</span></span>
<span class="line">      <span class="token operator">-></span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$command</span> <span class="token operator">=</span> <span class="token variable">$query</span><span class="token operator">-></span><span class="token function">createCommand</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$sql</span> <span class="token operator">=</span> <span class="token variable">$command</span><span class="token operator">-></span><span class="token property">sql</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$rows</span> <span class="token operator">=</span> <span class="token variable">$command</span><span class="token operator">-></span><span class="token function">queryAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>最重要的是，这些查询生成方法还可以和<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/db-active-record.md" target="_blank" rel="noopener noreferrer">活动记录</a>配合使用。</p>
<p>请参考<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/db-query-builder.md" target="_blank" rel="noopener noreferrer">查询生成器（Query Builder）</a>章节了解更多内容。</p>
<h2 id="活动记录-active-record" tabindex="-1"><a class="header-anchor" href="#活动记录-active-record"><span>活动记录（Active Record）</span></a></h2>
<p>Yii 2.0 的<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/db-active-record.md" target="_blank" rel="noopener noreferrer">活动记录</a>改动了很多。 两个最显而易见的改动分别涉及查询语句的生成（query building）和关联查询的处理（relational query handling）。</p>
<p>1.1 中的 <code v-pre>CDbCriteria</code> 类在 Yii 2 中被 [[yii\db\ActiveQuery]] 所替代。 这个类是继承自 [[yii\db\Query]]，因此也继承了所有查询生成方法。开始拼装一个查询可以调用 [[yii\db\ActiveRecord::find()]] 方法进行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 检索所有“活动的”客户和订单，并以 ID 排序：</span></span>
<span class="line"><span class="token variable">$customers</span> <span class="token operator">=</span> <span class="token class-name static-context">Customer</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'status'</span> <span class="token operator">=></span> <span class="token variable">$active</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">orderBy</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要声明一个关联关系，只需简单地定义一个 getter 方法来返回一个 [[yii\db\ActiveQuery|ActiveQuery]] 对象。 getter 方法定义的属性名代表关联表名称。 如，以下代码声明了一个名为 <code v-pre>orders</code> 的关系（1.1 中必须在 <code v-pre>relations()</code> 方法内声明关系）：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">Customer</span> <span class="token keyword">extends</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>db<span class="token punctuation">\</span>ActiveRecord</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">getOrders</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">hasMany</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Order'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'customer_id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>现在你就可以通过调用 <code v-pre>$customer-&gt;orders</code> 来访问关联表中某用户的订单了。 你还可以用以下代码进行一场指定条件的实时关联查询：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$orders</span> <span class="token operator">=</span> <span class="token variable">$customer</span><span class="token operator">-></span><span class="token function">getOrders</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">andWhere</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'status=1'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>当贪婪加载一段关联关系时，Yii 2.0 和 1.1 的运作机理并不相同。 具体来说，在 1.1 中使用一条 JOIN 语句同时查询主表和关联表记录。 在 Yii 2.0 中会使用两个没有 JOIN 的 SQL 语句：第一条语句取回主表记录， 第二条通过主表记录经主键筛选后查询关联表记录。</p>
<p>当生成返回大量记录的查询时，可以链式书写 [[yii\db\ActiveQuery::asArray()|asArray()]] 方法， 这样会以数组的形式返回查询结果，而不必返回 [[yii\db\ActiveRecord|ActiveRecord]] 对象，这能显著降低因大量记录读取所消耗的 CPU 时间和内存。如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$customers</span> <span class="token operator">=</span> <span class="token class-name static-context">Customer</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">asArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>另一个改变是你不能再通过公共变量定义属性（Attribute）的默认值了。 如果你需要这么做的话，可以在你的记录类的 <code v-pre>init</code> 方法中设置它们。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword static-context">parent</span><span class="token operator">::</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">status</span> <span class="token operator">=</span> <span class="token keyword static-context">self</span><span class="token operator">::</span><span class="token constant">STATUS_NEW</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>曾几何时，在 1.1 中重写一个活动记录类的构造方法会导致一些问题。它们不会在 2.0 中出现了。 需要注意的是，如果你需要在构造方法中添加一些参数，恐怕必须重写 [[yii\db\ActiveRecord::instantiate()]] 方法。</p>
<p>活动记录方面还有很多其他的变化与改进， 请参考<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/db-active-record.md" target="_blank" rel="noopener noreferrer">活动记录</a>章节以了解更多细节。</p>
<h2 id="活动记录行为-active-record-behaviors" tabindex="-1"><a class="header-anchor" href="#活动记录行为-active-record-behaviors"><span>活动记录行为（Active Record Behaviors）</span></a></h2>
<p>在 2.0 中遗弃了活动记录行为基类 <code v-pre>CActiveRecordBehavior</code>。 如果你想创建活动记录行为，需要直接继承 <code v-pre>yii\base\Behavior</code>。 如果行为类中需要表示一些事件，需要像这样覆写 <code v-pre>events()</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">namespace</span> <span class="token package">app<span class="token punctuation">\</span>components</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>db<span class="token punctuation">\</span>ActiveRecord</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>base<span class="token punctuation">\</span>Behavior</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">MyBehavior</span> <span class="token keyword">extends</span> <span class="token class-name">Behavior</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">events</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token class-name static-context">ActiveRecord</span><span class="token operator">::</span><span class="token constant">EVENT_BEFORE_VALIDATE</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'beforeValidate'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">beforeValidate</span><span class="token punctuation">(</span><span class="token variable">$event</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="用户及身份验证接口-identityinterface" tabindex="-1"><a class="header-anchor" href="#用户及身份验证接口-identityinterface"><span>用户及身份验证接口（IdentityInterface）</span></a></h2>
<p>1.1 中的 <code v-pre>CWebUser</code> 类现在被 [[yii\web\User]] 所取代，随之 <code v-pre>CUserIdentity</code> 类也不在了。 与之相对的，为达到相同目的，你可以实现 [[yii\web\IdentityInterface]] 接口，它使用起来更直观。 在高级应用模版里提供了一个这样的一个例子。</p>
<p>要了解更多细节请参考<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/security-authentication.md" target="_blank" rel="noopener noreferrer">认证（Authentication）</a>，<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/security-authorization.md" target="_blank" rel="noopener noreferrer">授权（Authorization）</a>以及<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/tutorial-advanced-app.md" target="_blank" rel="noopener noreferrer">高级应用模版</a> 这三个章节。</p>
<h2 id="url-管理" tabindex="-1"><a class="header-anchor" href="#url-管理"><span>URL 管理</span></a></h2>
<p>Yii 2.0 的 URL 管理跟 1.1 中很像。一个主要的改进是现在的 URL 管理支持<strong>可选参数</strong>了。 比如，如果你在 2.0 中定义了一个下面这样的规则，那么它可以同时匹配 <code v-pre>post/popular</code> 和 <code v-pre>post/1/popular</code> 两种 URL。 而在 1.1 中为达成相同效果，必须要使用两条规则。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'pattern'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'post/&lt;page:\d+>/&lt;tag>'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'route'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'post/index'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'defaults'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'page'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>请参考<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/runtime-url-handling.md" target="_blank" rel="noopener noreferrer">URL 解析和生成</a> 章节，以了解更多细节。.</p>
<p>一个重要变化是路由命名的约定， 现在的控制器和动作的驼峰命名会被转换成小写字母， 其中每个单词由一个专有的连字号分隔，例如，<code v-pre>CamelCaseController</code> 将为 <code v-pre>camel-case</code>。 有关更多详细信息，请参阅关于 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-controllers.md#controller-ids" target="_blank" rel="noopener noreferrer">controller IDs</a> 和 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-controllers.md#action-ids" target="_blank" rel="noopener noreferrer">action IDs</a> 的部分。</p>
<h2 id="同时使用-yii-1-1-和-2-x" tabindex="-1"><a class="header-anchor" href="#同时使用-yii-1-1-和-2-x"><span>同时使用 Yii 1.1 和 2.x</span></a></h2>
<p>如果你有一些遗留的 Yii 1.1 代码，需要跟 Yii 2.0 一起使用， 可以参考 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/tutorial-yii-integration.md" target="_blank" rel="noopener noreferrer">1.1 和 2.0 共用</a>章节。</p>
<blockquote>
<p>💖喜欢本文档的，欢迎点赞、收藏、留言或转发，谢谢支持！<br>
作者邮箱：zhuzixian520@126.com，github地址：<a href="https://github.com/zhuzixian520" target="_blank" rel="noopener noreferrer">github.com/zhuzixian520</a></p>
</blockquote>
</div></template>


